package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.TypesMattDao;
import com.fduss.artStore.entity.TypesMatt;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alex on 28/06/2017.
 */
public class TypesMattDaoImpl implements TypesMattDao {
    private JdbcUtil util;

    public TypesMattDaoImpl() {
        util = new JdbcUtil();
    }

    @Override
    public TypesMatt add(TypesMatt typesMatt) {
        Connection conn = util.getConnection();
        String sql = "INSERT INTO `art`.`typesmatt` (`MattID`, `Title`, `ColorCode`) VALUES (?, ?, ?)";
        PreparedStatement pst = null;
        boolean flag = false;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesMatt.getMattId());
            pst.setString(2, typesMatt.getTitle());
            pst.setString(3, typesMatt.getColorCode());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
        return flag ? typesMatt : null;
    }

    @Override
    public void delete(int typesMattId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`typesmatt` WHERE `MattID` = ?";
        PreparedStatement pst = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesMattId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public TypesMatt getOne(int typesMattId) {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`typesmatt` WHERE `MattID` = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        TypesMatt matt = null;
        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesMattId);
            rs = pst.executeQuery();
            if (rs.next()){
                matt = buildMatt(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return matt;
    }

    @Override
    public List<TypesMatt> getAll() {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`typesmatt`";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<TypesMatt> matts = new ArrayList<>();
        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()){
                matts.add(buildMatt(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return matts;
    }

    private TypesMatt buildMatt(ResultSet rs) throws SQLException {
        TypesMatt matt = new TypesMatt();
        matt.setColorCode(rs.getString("ColorCode"));
        matt.setMattId(rs.getInt("MattID"));
        matt.setTitle(rs.getString("Title"));
        return matt;
    }
}
